package com.ad.pet;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.ad.pet.common.data.http.HttpHelp;
import com.ad.pet.common.data.injector.component.ApplicationComponent;
import com.ad.pet.common.data.injector.component.DaggerApplicationComponent;
import com.ad.pet.common.data.injector.module.ApiModule;
import com.ad.pet.common.data.injector.module.ApplicationModule;
import com.ad.pet.common.data.utils.FileUtil;
import com.ad.pet.common.data.utils.upload.UploadFiles;
import com.ad.pet.common.structure.MainThreadHandler;
import com.blankj.utilcode.util.Utils;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import javax.inject.Inject;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by ems on 2017/2/27.
 * app 入口
 */

public class App extends MultiDexApplication {

    private static App app;
    private ApplicationComponent applicationComponent;

    public static String longTitu="121.48";
    public static String latitude="31.22";
    public static String phone="";
    public static String username="";
    @Inject
    UploadFiles mUploadFiles;
    @Inject
    HttpHelp mHttpHelp;

    @Override
    public void onCreate() {
        super.onCreate();
        this.app = this;
        init();

//        initLeakCanary();
    }

    private void init() {
        initInjector();
        Logger.setContext(this);
        CrashHandler.getInstance().init(this, mUploadFiles);
////        FileUtil fileUtil = new FileUtil(this, new ResourcesUtil(this));
////        LoggerPrint.filePath(fileUtil.getPrintLogDir().getPath());
        MainThreadHandler.initLoop(getMainLooper());
        ZXingLibrary.initDisplayOpinion(this);
//        ContextUtility.setApplication(this);
        Utils.init(this);
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallback());
        MultiDex.install(this);
        initUM();
//        initUpgradeDialog();
        initCloudChannel(this);


//        AlibcTradeSDK.asyncInit(this, new AlibcTradeInitCallback() {
//            @Override
//            public void onSuccess() {
//                //初始化成功，设置相关的全局配置参数
//                // ...
//                Logger.e("初始化阿里电商成功");
//            }
//            @Override
//            public void onFailure(int code, String msg) {
//                //初始化失败，可以根据code和msg判断失败原因，详情参见错误说明
//                Logger.e("初始化阿里电商失败："+code+msg);
//            }
//        });



    }



    private void initUM() {
        UMConfigure.init(this,"5c2f1442f1f556deb000008f","umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
        UMConfigure.setLogEnabled(BuildConfig.DEBUG);

        PlatformConfig.setWeixin("wxd626c7623716e799","87dbf2eaeb429885ebd9c3ba90c476c9");
//        PlatformConfig.setQQZone("101585668","6c1179f5e84c734bbbdf1" +
//                "b752af9c208");




    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
//        Beta.installTinker();
        // 安装tinker


    }

//    private void initLeakCanary() {
//        if (LeakCanary.isInAnalyzerProcess(this)) {
//            return ;
//        }
//        LeakCanary.install(this);
////        return false;
//    }

    public void initInjector() {
        applicationComponent = DaggerApplicationComponent.builder().apiModule(new ApiModule())
                .applicationModule(new ApplicationModule(app.getApplicationContext()))
                .build();
        applicationComponent.inject(this);
    }

    public void initLogger() {
        com.ad.pet.Logger.config(new com.ad.pet.Logger.Builder()
                .setLog2FileSwitch(false)
                .setDir(FileUtil.getInstance().getLogDir())
                .setLevel(com.ad.pet.Logger.D));
    }

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }

    public static App getApp() {
        return app;
    }

    private void initUpgradeDialog() {
//        /**
//         * 自定义初始化开关
//         */
//        Beta.autoInit = true;
//        /**
//         * true表示初始化时自动检查升级; false表示不会自动检查升级,需要手动调用Beta.checkUpgrade()方法;
//         */
//        Beta.autoCheckUpgrade = true;
//
//
//        Beta.canShowUpgradeActs.add(MainActivity.class);
//        Bugly.init(this, "536a9b9062", BuildConfig.DEBUG);

    }

    /**
     * 初始化云推送通道
     *
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        JPushInterface.init(applicationContext);
//        PushServiceFactory.init(applicationContext);
//        CloudPushService pushService = PushServiceFactory.getCloudPushService();
//        pushService.register(applicationContext, new CommonCallback() {
//            @Override
//            public void onSuccess(String response) {
//                LogUtils.d("init cloudchannel success");
//            }
//
//            @Override
//            public void onFailed(String errorCode, String errorMessage) {
//                LogUtils.d("init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
//            }
//        });


    }

}
